package tothreedesignmodel;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;

/**
 * @author luozh
 * @description
 * @date 创建于 9:31 2020/9/1
 * @modified
 */
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int total = sc.nextInt();
        int passLine = sc.nextInt();
        int priority = sc.nextInt();
        int i = 0;
        ArrayList<Student> list = new ArrayList<>();
        while (i++ < total) {
            list.add(new Student(sc.nextInt(), sc.nextInt(), sc.nextInt()));
        }
        list.stream().filter(item -> item.getDe() >= passLine && item.getCai() >= passLine).
                filter(item -> item.getDe() >= priority && item.getCai() >= priority).sorted(
                Comparator.comparing(Student::getAll, (x, y) -> {
                    if (x > y) {
                        return -1;
                    } else if (x.equals(y)) {
                        return 0;
                    } else {
                        return 1;
                    }
                }).thenComparing(Student::getDe, (x, y) -> {
                    if (x > y) {
                        return -1;
                    } else if (x.equals(y)) {
                        return 0;
                    } else {
                        return 1;
                    }
                }).thenComparing(Student::getNum, (x, y) -> {
                    if (x > y) {
                        return 1;
                    } else {
                        return -1;
                    }
                })).forEach(System.out::println);
        list.stream().filter(item -> item.getDe() >= passLine && item.getCai() >= passLine).
                filter(item -> !(item.getDe() >= priority && item.getCai() >= priority)&&item.getDe()>=priority).sorted(
                Comparator.comparing(Student::getAll, (x, y) -> {
                    if (x > y) {
                        return -1;
                    } else if (x.equals(y)) {
                        return 0;
                    } else {
                        return 1;
                    }
                }).thenComparing(Student::getDe, (x, y) -> {
                    if (x > y) {
                        return -1;
                    } else if (x.equals(y)) {
                        return 0;
                    } else {
                        return 1;
                    }
                }).thenComparing(Student::getNum, (x, y) -> {
                    if (x > y) {
                        return 1;
                    } else {
                        return -1;
                    }
                })).forEach(System.out::println);
        list.stream().filter(item -> item.getDe() >= passLine && item.getCai() >= passLine).
                filter(item -> !((item.getDe() >= priority && item.getCai() >= priority)||(!(item.getDe() >= priority && item.getCai() >= priority)&&item.getDe()>=priority))).sorted(
                Comparator.comparing(Student::getAll, (x, y) -> {
                    if (x > y) {
                        return 1;
                    } else if (x.equals(y)) {
                        return 0;
                    } else {
                        return -1;
                    }
                }).thenComparing(Student::getDe, (x, y) -> {
                    if (x > y) {
                        return -1;
                    } else if (x.equals(y)) {
                        return 0;
                    } else {
                        return 1;
                    }
                }).thenComparing(Student::getNum, (x, y) -> {
                    if (x > y) {
                        return 1;
                    } else {
                        return -1;
                    }
                })).forEach(System.out::println);

    }
}

class Student {
    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public int getDe() {
        return de;
    }

    public int getAll() {
        return getDe() + getCai();
    }

    public void setDe(int de) {
        this.de = de;
    }

    public int getCai() {
        return cai;
    }

    public void setCai(int cai) {
        this.cai = cai;
    }

    private int num;
    private int de;
    private int cai;

    public Student(int num, int de, int cai) {
        this.num = num;
        this.de = de;
        this.cai = cai;
    }

    @Override
    public String toString() {
        return String.format("%d %d %d", num, de, cai);
    }
}
